import es.abredatos.energyes.*
import grails.util.GrailsUtil

class BootStrap {
	
	def dataService
	def grailsApplication
	
	private static final REGIONS = [
			[name:'Andalucía', code:'ANDALUCIA', lat:37.544577, lng:-4.724121, provinces:[
				[name:'Almería', code:'ALMERIA'],
				[name:'Cádiz', code:'CADIZ'],
				[name:'Córdoba', code:'CORDOBA'],
				[name:'Granada', code:'GRANADA'],
				[name:'Huelva', code:'HUELVA'],
				[name:'Jaen', code:'JAEN'],
				[name:'Malaga', code:'MALAGA'],
				[name:'Sevilla', code:'SEVILLA']
				]],
			[name:'Aragón', code:'ARAGON', lat:41.795888, lng:-0.856934, provinces:[
				[name:'Huesca', code:'HUESCA'],
				[name:'Teruel', code:'TERUEL'],
				[name:'Zaragoza', code:'ZARAGOZA']
				]],
			[name:'Canarias', code:'CANARIAS', lat:28.526622, lng:-16.589355, provinces:[
				[name:'Las Palmas', code:'LAS PALMAS'],
				[name:'Santa Cruz de Tenerife', code:'S.C.TENERIFE']
				]],
			[name:'Cantabria', code:'CANTABRIA', lat:43.181147, lng:-3.988037, provinces:[
				[name:'Cantabria', code:'CANTABRIA']
				]],
			[name:'Castilla La Mancha', code:'CASTILLA LA MANCHA', lat:39.147103, lng:-3.120117, provinces:[
				[name:'Albacete', code:'ALBACETE'],
				[name:'Ciudad Real', code:'CIUDAD REAL'],
				[name:'Cuenca', code:'CUENCA'],
				[name:'Guadalajara', code:'GUADALAJARA'],
				[name:'Toledo', code:'TOLEDO']
				]],
			[name:'Castilla León', code:'CASTILLA LEON', lat:41.836828, lng:-4.394531, provinces:[
				[name:'Ávila', code:'AVILA'],
				[name:'Burgos', code:'BURGOS'],
				[name:'León', code:'LEON'],
				[name:'Palencia', code:'PALENCIA'],
				[name:'Salamanca', code:'SALAMANCA'],
				[name:'Segovia', code:'SEGOVIA'],
				[name:'Soria', code:'SORIA'],
				[name:'Valladolid', code:'VALLADOLID'],
				[name:'Zamora', code:'ZAMORA']
				]],
			[name:'Cataluña', code:'CATALUÑA', lat:41.795888, lng:1.505127, provinces:[
				[name:'Barcelona', code:'BARCELONA'],
				[name:'Girona', code:'GIRONA'],
				[name:'Lleida', code:'LLEIDA'],
				[name:'Tarragona', code:'TARRAGONA']
				]],
			[name:'Madrid', code:'COMUNIDAD DE MADRID', lat:40.396764, lng:-3.713379, provinces:[
				[name:'Madrid', code:'MADRID']
				]],
			[name:'Navarra', code:'COM.FORAL DE NAVARRA', lat:42.584937, lng:-1.347949, provinces:[
				[name:'Navarra', code:'NAVARRA']
				]],
			[name:'Valencia', code:'COMUNID. VALENCIANA', lat:39.647997, lng:-0.318604, provinces:[
				[name:'Alicante', code:'ALICANTE'],
				[name:'Castellón', code:'CASTELLON'],
				[name:'Valencia', code:'VALENCIA']
				]],
			[name:'Extremadura', code:'EXTREMADURA', lat:39.495563, lng:-6.064453, provinces:[
				[name:'Badajoz', code:'BADAJOZ'],
				[name:'Cáceres', code:'CACERES']
				]],
			[name:'Galicia', code:'GALICIA', lat:42.577355, lng:-8.129883, provinces:[
				[name:'La Coruña', code:'LA CORUÑA'],
				[name:'Lugo', code:'LUGO'],
				[name:'Orense', code:'ORENSE'],
				[name:'Pontevedra', code:'PONTEVEDRA']
				]],
			[name:'Islas Baleares', code:'ISLAS BALEARES', lat:39.53794, lng:2.856445, provinces:[
				[name:'Baleares', code:'BALEARES']
				]],
			[name:'La Rioja', code:'LA RIOJA', lat:42.285437, lng:-2.537842, provinces:[
				[name:'La Rioja', code:'LA RIOJA']
				]],
			[name:'País Vasco', code:'PAIS VASCO', lat:42.988576, lng:-2.614746, provinces:[
				[name:'Álava', code:'ALAVA'],
				[name:'Guipuzcoa', code:'GUIPUZCOA'],
				[name:'Vizcaya', code:'VIZCAYA']
				]],
			[name:'Principado de Asturias', code:'PRINCIP. DE ASTURIAS', lat:43.245203, lng:-5.976562, provinces:[
				[name:'Asturias', code:'ASTURIAS']
				]],
			[name:'Región de Murcia', code:'REGION DE MURCIA', lat:37.983175, lng:-1.126099, provinces:[
				[name:'Murcia', code:'MURCIA']
				]],
			[name:'Ceuta', code:'CEUTA', lat:35.88905, lng:-5.306396, provinces:[
				[name:'Ceuta', code:'CEUTA']
				]],
			[name:'Melilla', code:'MELILLA', lat:35.290469, lng:-2.93335, provinces:[
				[name:'Melilla', code:'MELILLA']
				]]
		]
	
    def init = { servletContext ->
		addMetaMethods()
		
		switch (GrailsUtil.environment) {
			case ["development"]:
				addDevelopmentData()
				break
			case ["production","beta"]:
				addProductionData()
				break
			case "test":
				addTestData()
				break
			default:
				assert(false)
		}
    }
	
	private void addMetaMethods() {}
	
	private void addProductionData() {
		if (!Region.count() && !Province.count()) {
			addRegionsAndProvinces()
		}
		addEnergyData()
	}
	
	private void addTestData() {
		addRegionsAndProvinces()
		addEnergyData([2009],null)
	}
	
	private void addDevelopmentData() {
		addRegionsAndProvinces()
		addEnergyData([2010],null)
	}
	
	private void addRegionsAndProvinces() {
		Region region
		Province province
		
		log.debug("Creating the regions...")
		
		REGIONS.each { currentRegion ->
			region = new Region(name:currentRegion.name, code:currentRegion.code, lat:currentRegion.lat, lng:currentRegion.lng)
			
			currentRegion.provinces.each { currentProvince ->
				province = new Province(name:currentProvince.name, code:currentProvince.code)
				region.addToProvinces(province)
			}
				
			if(!region.save()) {
				log.error("Error saving the region ${currentRegion}")
				region.errors.each{log.errog(it)}
			}
		}
		log.debug("Regions created!")
		
		Region.list().each { currentRegion ->
			log.debug("Region: ${currentRegion}, provinces:${currentRegion.provinces}")
		}
	}
	
	private void addEnergyData(){
		addEnergyData(null, null)
	}
	
	private void addEnergyData(years,months){
		years = years ?: grailsApplication.config.energy.data.initLoad.years 
		months = months ?: grailsApplication.config.energy.data.initLoad.months
		log.debug("Add Energy Data for years:${years} and months:${months}")	
		dataService.importDataForInterval(years, months)
	}
	
    def destroy = {
    }
}
